//
//  DLAddGroupVC.m
//  AllPass
//
//  Created by apple on 2021/11/2.
//

#import "DLAddGroupVC.h"
#import "DLIconChoseVC.h"
#import "ImageFactory.h"
@interface DLAddGroupVC ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topHeight;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property(nonatomic,assign)NSInteger imgIdx;
@end

@implementation DLAddGroupVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imgIdx = 48;
    self.myTitle = NSLocalizedString(@"Editing Group", nil);
    self.view.backgroundColor = UnderLineColor;
    self.topHeight.constant = kTopHeight + 20;
    self.leftBtnImage = @"navigationbar_back_image";
}
- (void)backClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)choseIconClick:(id)sender {
    DLIconChoseVC * vc = [DLIconChoseVC new];
    vc.choseIconIdxBlock = ^(NSInteger idx) {
        UIImage *image = [[ImageFactory sharedInstance] imageForIndex:idx];
        self.iconView.image = image;
        self.imgIdx = idx;
    };
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)doneClick:(id)sender {
    DatabaseDocument * db = [[VicSingleObject getInstance]db];
    KdbGroup *g = [db.kdbTree createGroup:db.kdbTree.root];//组暂时定为只能在根目录添加
    g.name = self.nameTF.text;
    g.image = self.imgIdx;
    [db.kdbTree.root addGroup:g];
    [db save];
    if (self.doneBlock) {
        self.doneBlock();
    }
    [self backClick];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
